package com.testitems.service.user;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.testitems.common.web.page.PageInfo;
import com.testitems.config.security.UserInfo;
import com.testitems.controller.user.dto.UserCreateRequest;
import com.testitems.controller.user.dto.UserSearchRequest;
import com.testitems.controller.user.dto.UserUpdateRequest;
import com.testitems.repo.entity.User;
import com.testitems.repo.mapper.UserMapper;
import com.testitems.repo.mapper.ext.UserExtMapper;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.stereotype.Service;

import java.util.Date;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author GFH
 * @since 2021-11-20
 */
@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements IUserService {

    @Autowired
    private UserMapper userMapper;
    @Autowired
    private UserExtMapper userExtMapper;
    @Autowired
    private PasswordEncoder passwordEncoder;

    @Override
    public PageInfo search(UserSearchRequest request) {
        Page page = new Page(request.getCurrent(), request.getSize());
        LambdaQueryWrapper<User> queryWrapper = new LambdaQueryWrapper<>();
        if (StringUtils.isNotBlank(request.getName())) {
            queryWrapper.eq(User::getName, request.getName());
        }
        if (request.getRoleLevel() != null) {
            queryWrapper.eq(User::getRoleLevel, request.getRoleLevel());
        }
        queryWrapper.eq(User::getIsDelected, false);
        IPage userPage = userMapper.selectPage(page, queryWrapper);
        return PageInfo.get(userPage);
    }

    @Cacheable(cacheNames = CACHE_USER_5MINS, key = "#username")
    @Override
    public User getByUserNameFromCache(String username) {
        return this.getByUserName(username);
    }

    @Override
    public User getByUserName(String userName) {
        LambdaQueryWrapper<User> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(User::getUsername, userName);
        queryWrapper.eq(User::getStatus, true);
        queryWrapper.eq(User::getIsDelected, false);
        return userMapper.selectOne(queryWrapper);
    }

    @Override
    public void createdUser(UserCreateRequest request, UserInfo userInfo) {
        User user = new User();
        BeanUtils.copyProperties(request, user);
        user.setPassword(passwordEncoder.encode(request.getPassword()));
        user.setCreatedTime(new Date());
        user.setCreatedBy(userInfo.getName());
        user.setCreatedById(userInfo.getId());
        userMapper.insert(user);
    }

    @Override
    public void updateByUserId(Integer id, UserUpdateRequest request, UserInfo userInfo) {
        User user = super.getById(id);
        user.setName(request.getName());
        if (StringUtils.isNotBlank(request.getPassword())) {
            user.setPassword(passwordEncoder.encode(request.getPassword()));
        }
        user.setRoleLevel(request.getRoleLevel());
        user.setStatus(request.getStatus());
        super.updateById(user);
    }

    @Override
    public void logicDelete(Integer id, UserInfo userInfo) {
        User user = super.getById(id);
        user.setIsDelected(true);
//        user.setUpdatedBy(userInfo.getName());
//        user.setUpdatedById(userInfo.getId());
        user.setUpdatedTime(new Date());
        super.updateById(user);
    }
}
